﻿using IdentityModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Claims;
using System.Threading.Tasks;

namespace WebOSCore.Models
{
    public class Unity
    {
        public enum SysOperation
        {//填加系统时间
            _SYS_DATETIME,
            //当前用户ID
            _SYS_GETUSERID,
            //当前用户名
            _SYS_GETUSERNAME,
            //当前昵称
            _SYS_GETUSERNICKNAME,
        }
        /// <summary>
        /// 填加系统时间后期可补充
        /// </summary>
        /// <param name="json">操作josn</param>
        /// <returns>操作后的json</returns>
        public static string OperationJson(string json)
        {
            json=json.Replace(SysOperation._SYS_DATETIME.ToString(), DateTime.Now.ToString());
            return json;
        }
        /// <summary>
        /// 填加认证信息
        /// </summary>
        /// <param name="v"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        internal static string OperationJson(string v, ClaimsPrincipal user)
        {
            v=v.Replace(SysOperation._SYS_DATETIME.ToString(), DateTime.Now.ToString());
            //得到当前用户ID
            if (user.Claims.FirstOrDefault(c => c.Type == JwtClaimTypes.Id.ToString()) != null)
            {
                v=v.Replace(SysOperation._SYS_GETUSERID.ToString(), user.Claims.FirstOrDefault(c => c.Type == JwtClaimTypes.Id.ToString()).Value);
            }
            //得到当前用户名称
            if (user.Claims.FirstOrDefault(c => c.Type == JwtClaimTypes.Name.ToString()) != null)
            {
                v= v.Replace(SysOperation._SYS_GETUSERNAME.ToString(), user.Claims.FirstOrDefault(c => c.Type == JwtClaimTypes.Name.ToString()).Value);
            }
            //得到当前用户名昵称
            if (user.Claims.FirstOrDefault(c => c.Type == JwtClaimTypes.NickName.ToString()) != null)
            {
                v= v.Replace(SysOperation._SYS_GETUSERNICKNAME.ToString(), user.Claims.FirstOrDefault(c => c.Type == JwtClaimTypes.NickName.ToString()).Value);
            }
            return v;
        }
        /// <summary>
        /// 将对象转字典
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Dictionary<object, object> ObjModel(object obj)
        {
            var dic = new Dictionary<object, object>();
            Type t = obj.GetType();//获得该类的Type

            //如果是字典项
            if (t.Name.Contains("Dictionary"))
            {
                return obj as Dictionary<object, object>;
            }
            //这是实体模型
            else
            {
                foreach (PropertyInfo pi in t.GetProperties())
                {
                    var name = pi.Name;//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作
                    var value = pi.GetValue(obj, null);//用pi.GetValue获得值
                    var type = value?.GetType() ?? typeof(object);//获得属性的类型
                    dic.Add(name, value == null ? "" : value);


                }
            }

            return dic;
        }

        /// <summary>
        /// 将json转为字典项list，转换失败返回null
        /// </summary>
        /// <param name="json">为list的json</param>
        /// <returns></returns>
        public static List<Dictionary<object, object>> JsonToListDic(string json) {
            try {  
                var art = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dictionary<object, object>>>(json);
            return art; } catch { return null; }
          
        }
        /// <summary>
        /// json转为字典项转换失败返回null
        /// </summary>
        /// <param name="json">单个json</param>
        /// <returns></returns>
        public static Dictionary<object, object> JsonToDic(string json)
        {
            try
            {
                var art = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<object, object>>(json);
                return art;
            }
            catch { return null; }

        }
    }
}
